var app = getApp()
var util = require("../../utils/util.js")
var orderModel = require("../../model/orderModel.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: null,
    refundCauses: null,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.init()
    this.setData({
      order: app.data.tempOrder
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  init() {
    var that = this
    util.showLoading("加载中")
    orderModel.refundCause(res => {
      wx.hideLoading()
      if (res.status == 1) {
        util.failToast(res.msg)
        return
      }
      that.setData({
        refundCauses: res.data
      })
    })
  },
  bindSubmit(e) {
    var value = e.detail.value
    var refund = value.refund
    var remark = value.remark
    if (remark == "" && refund.length == 0) {
      util.failToast("至少选择一项")
      return
    }
    var data = []
    for (var i = 0; i < refund.length; i++) {
      var arr = refund[i].split(",")
      var obj = {
        rcId: parseInt(arr[0]),
        content: arr[1]
      }
      data.push(obj)
    }
    if (remark != "")
      data.push({
        rsId: 0,
        content: remark
      })
    var req = {
      cause: JSON.stringify(data),
      orderId: this.data.order.id
    }
    util.showLoading("申请中")
    orderModel.refundApply(req, res => {
      wx.hideLoading()
      if (res.status == 1) {
        util.failToast(res.msg)
        return
      }
      util.successToast(res.msg)
      app.data.tempOrder.status=-1
      app.data.tempOrder.statusStr="待退款"
      setTimeout(()=>{
        wx.navigateBack()
      },800)
    })
  }
})